const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
let router = new Router()

const mongoose = require('mongoose')
const fs = require('fs')

//导入所有的数据  newGoods.json是提纯以后的数据
router.get('/insertAllGoodsInfo',async (ctx)=>{
    fs.readFile('./data_json/newGoods.json','utf8',(err,data)=>{
        data = JSON.parse(data)
        let count = 0 
        const Goods = mongoose.model('Goods')
        data.map((value,index)=>{
            console.log(value)
            let newGoods = new Goods(value)
            newGoods.save().then(()=>{
                count++
                console.log("成功")
            }).catch(()=>{
                console.log("失败")
            })
        })
    })

    ctx.body = "开始导入数据"
})

//导入商品大类的数据
router.get('/insertAllCategory',async (ctx)=>{
    fs.readFile('./data_json/category.json','utf8',(err,data)=>{
        data = JSON.parse(data)
        let count = 0
        const Category = mongoose.model('Category')
        data.RECORDS.map((value,index)=>{
            console.log(value)
            let newCategory = new Category(value)
            newCategory.save().then(()=>{
                count++
                console.log('成功'+count)
            }).catch(error=>{
                 console.log('失败：'+error)
            })
        })
    })
    ctx.body = "开始导入数据2"
})

//导入商品小类的数据
router.get('/insertAllCategorySub',async (ctx)=>{
    fs.readFile('./data_json/category_sub.json','utf8',(err,data)=>{
        data = JSON.parse(data)
        let count = 0 
        const CategorySub = mongoose.model('categorySub')
        data.RECORDS.map((value,index)=>{
            console.log(value)
            let newcategorySub = new CategorySub(value)
            newcategorySub.save().then(()=>{
                count++
                console.log("插入成功"+count)
            }).catch(error=>{
                console.log(error)
            })
        })
    })
    ctx.body = "开始导入数据3"
})

//热卖商品 详情
router.post('/getDetailGoodsInfo',async(ctx)=>{
    try{
        let goodsId = ctx.request.body.goodsId //获取传递的goodsId的参数
        const Goods = mongoose.model('Goods')
        let result = await Goods.findOne({ID:goodsId}).exec()
        ctx.body = {code:200,message:result}
    }catch(err){
        ctx.body = {code:500,message:err}
    }
})

//商品分类
router.get('/getCategoryList',async(ctx)=>{
    try{
        let Category = mongoose.model('Category')
        let result = await Category.find().exec()
        ctx.body = {
            code:200,
            message:result
        }
    }catch(error){
        ctx.body = {
            code:500,
            message:error
        }
    }
})

//商品分类  二级类别
router.post('/getCategorySubList',async(ctx)=>{
    try{
        let categoryId = ctx.request.body.categoryId
        let categorySub = mongoose.model('categorySub')
        let result = await categorySub.find({MALL_CATEGORY_ID:categoryId}).exec()
        ctx.body = {
            code:200,
            message:result
        }
    }catch(error){
        ctx.body = {
            code:500,
            message:error
        }
    }
})

//根据商品类别获取商品列表
router.post('/getGoodsListByCategorySubID',async(ctx)=>{
    try{
        let categorySubId  = ctx.request.body.categorySubId  //类别
        let page = ctx.request.body.page // 获取传递的页码
        let num = 10 //每一页显示的数量
        let start = (page-1) * num //当前从什么位置开始读取数据

        let Goods = mongoose.model('Goods')
        // .skip(数字)跳过多少个数据然后在读取数据 .limit(数字)读取指定数量的数据
        let result = await Goods.find({SUB_ID:categorySubId}).skip(start).limit(num).exec()
        ctx.body = {
            code:200,
            message:result
        }
    }catch(error){
        ctx.body = {
            code:500,
            message:error
        }
    }
})

module.exports = router